package com.wxw.BaseLibrary.http.soap;

import org.apache.http.Header;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2016/5/9.
 */
public class SoapResponse<T> implements Response {
    private String url;
    private RequestMethod method;
    private Map<String, String> headers;
    private int responseCode;
    private byte[] responseData;
    private T result;
    private Object tag;
    private Exception exception;
    private long mNetworkMillis;

    public SoapResponse(String url, RequestMethod method, Map<String, String> headers, int responseCode, byte[] byteData,
                        T result, Object tag, long networkMillis, Exception exception) {
        this.url = url;
        this.method = method;
        this.headers = headers;
        this.responseCode = responseCode;
        this.responseData = byteData;
        this.result = result;
        this.tag = tag;
        this.mNetworkMillis = networkMillis;
        this.exception = exception;
    }

    public SoapResponse(String url, RequestMethod method, Object tag, long networkMillis, Exception exception) {
        this(url, method, null, 0, null, null, tag, networkMillis, exception);
    }

    @Override
    public String url() {
        return url;
    }

    @Override
    public RequestMethod getRequestMethod() {
        return method;
    }

    @Override
    public boolean isSucceed() {
        return responseCode== HttpURLConnection.HTTP_OK;
    }

    @Override
    public Map<String, String> getHeaders() {
        return headers;
    }

    @Override
    public int getResponseCode() {
        return responseCode;
    }

    @Override
    public byte[] getByteArray() {
        return responseData;
    }

    @Override
    public T get() {
        return result;
    }

    @Override
    public Exception getException() {
        return exception;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public long getNetworkMillis() {
        return mNetworkMillis;
    }

    	/* ====================Read response body=================== */

    /**
     * To read information from the server's response.
     *
     * @param inputStream outputStream from the service, for us is the inputStream, the data read from the inputStream.
     * @return Data from server.
     * @throws IOException To read data when possible.
     */
    public static byte[] readResponseBody(InputStream inputStream) throws IOException {
        int readBytes;
        byte[] buffer = new byte[1024];
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        while ((readBytes = bufferedInputStream.read(buffer)) != -1) {
            content.write(buffer, 0, readBytes);
        }
        content.flush();
        content.close();
        return content.toByteArray();
    }

    /**
     * This requestMethod and responseCode has ResponseBody ?
     *
     * @param requestMethod it's come from {@link com.yolanda.nohttp.RequestMethod}.
     * @param responseCode  responseCode from server.
     * @return True: there is data, false: no data.
     */
    public static boolean hasResponseBody(com.yolanda.nohttp.RequestMethod requestMethod, int responseCode) {
        return requestMethod != com.yolanda.nohttp.RequestMethod.HEAD && hasResponseBody(responseCode);
    }

    /**
     * According to the response code to judge whether there is data.
     *
     * @param responseCode responseCode.
     * @return True: there is data, false: no data.
     */
    public static boolean hasResponseBody(int responseCode) {
        return !(100 <= responseCode && responseCode < 200) && responseCode != 204 && responseCode != 205 && responseCode != 304;
    }
}
